package cn.tedu.crm.deposit.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.tedu.crm.base.pojo.PageObject;
import cn.tedu.crm.base.response.JsonResult;
import cn.tedu.crm.customer.service.CustomerService;
import cn.tedu.crm.deposit.pojo.dto.DepositQuery;
import cn.tedu.crm.deposit.pojo.dto.DepositSaveParam;
import cn.tedu.crm.deposit.pojo.vo.*;
import cn.tedu.crm.deposit.service.DepositService;
import cn.tedu.crm.orders.service.OrdersService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Api(tags = "财务管理定金")
@RestController
@RequestMapping("/api/crm/deposit")
public class DepositController {
    @Autowired
    private DepositService depositService;

    @Autowired
    private CustomerService customerService;

    @Autowired
    private OrdersService ordersService;

    @SaCheckPermission("crm:finance:deposit:select")
    @GetMapping("/select")
    public JsonResult selectDepositList(DepositQuery depositQuery){
        if (depositQuery.getId()==null){
            PageObject<DepositVO> pageObject = PageObject.page(
                    depositQuery.getPageNum(), depositQuery.getPageSize(),
                    ()->depositService.selectDepositList(depositQuery));
            return JsonResult.ok(pageObject);
        }
        return JsonResult.ok();
    }
    //录入表回显查询
    @SaCheckPermission("crm:finance:deposit:select")
    @GetMapping("/selectById/{id}")
    public JsonResult selectDepositById(@PathVariable Long id){
        log.debug("回显查询id:{}",id);
        CustomerVO cust = customerService.selectId(id);
        return JsonResult.ok(cust);
    }
    //录入表插入
    @SaCheckPermission("crm:finance:deposit:add")
    @PostMapping("/insert")
    public JsonResult insertDeposit(@RequestBody DepositSaveParam depositSaveParam){
        log.debug("数据{}:",depositSaveParam);
        depositService.insertDeposit(depositSaveParam);
        return JsonResult.ok();
    }
    //查询订单表
    @SaCheckPermission("crm:finance:deposit:select")
    @GetMapping("/selectOrdersById/{id}")
    public JsonResult selectOrdersById(@PathVariable Long id){
        log.debug("查看顶级表的id服务层:{}",id);
        OrdersDepositVO orders =  ordersService.selectOrdersById(id);
        return JsonResult.ok(orders);
    }

    @GetMapping("/selectDepositById/{id}")
    public JsonResult selectById(@PathVariable Long id){
        log.debug("查询定金表服务层:{}",id);
        DepositVOO depositVOO = depositService.selectById(id);
        return JsonResult.ok(depositVOO);
    }
    //查询工程订单信息
    @SaCheckPermission("crm:finance:project:select")
    @GetMapping("/selectProjectById/{id}")
    public JsonResult selectPeojectById(@PathVariable Long id){
        log.debug("工程订单信息{}",id);
        ProjectVO projectVO = ordersService.selectPeojectById(id);
        return JsonResult.ok(projectVO);

    }

}
